import { defineConfig, loadEnv } from 'vite';
import path from 'path';
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"
import svgLoader from "vite-svg-loader";
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: './',
    plugins: [
      vue(),
      vueJsx(),
      /** 将 SVG 静态图转化为 Vue 组件 */
      svgLoader(),
      /** SVG */
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), "src/assets/icon")],
        symbolId: "icon-[name]"
      }),
      VueSetupExtend()
    ],
    server: {
      host: true,
      port: 9000,
      open: false,
      hmr: true, //热更新
      cors: true,
      proxy: {
        '/api': {
          // target: 'https://test.inspection.livat.com/api',
          target: 'https://api-inspection-test.livat.cn',
          // target: 'http://172.17.17.87:8099', // cai
          // target: 'http://172.17.17.125:8099', // ethen
          // target: 'http://172.17.17.62:8099', //jue
          rewrite: (path) => path.replace(/^\/api/, ''),

          // 默认true情况下，不接受运行在 HTTPS 上，且使用了无效证书的后端服务器
          secure: false,
          changeOrigin: true,
          configure: (proxy, options) => {
            proxy.on('proxyReq', function (proxyReq, req, res) {
              proxyReq.removeHeader('referer')  //移除请求头
              proxyReq.removeHeader('origin') //移除请求头
            });
          }
        },

      },
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
    },
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: env.VITE_ENV === 'dev' ? false : true,
          drop_debugger: env.VITE_ENV === 'dev' ? false : true,
        },
      },
    },
  }
})
